import java.sql.ResultSet;
import java.sql.SQLException;

import mysql.Mysql;


/**
 * Class Component
 */
public class Component {

	//
	// Fields
	//

	private int id;
	private User user;

	private String name;
	private String type;
	private String image;
	private int life;
	private int damage = 0;
	private int attack;
	private int range;
	private int cost;
	private int size;
	private int travel;
	private int point;

	//
	// Constructors
	//
	public Component(String type, User user) {
		this.user = user;
		if (type.toLowerCase().equals("castle")) {
			this.setName("Castle");
			this.type = "castle";
			this.image = "castle.png";
			this.life = 10000;
			this.attack = 15;
			this.range = 10;
			this.cost = 1000000;
			this.size = 1;
			this.travel = 0;
			this.point = 0;
		}

		else if (type.toLowerCase().equals("soldier")) {
			this.setName("Soldier");
			this.type = "soldier";
			this.image = "soldier.png";
			this.life = 100;
			this.attack = 10;
			this.range = 1;
			this.cost = 50;
			this.size = 1;
			this.travel = 2;
			this.point = 0;
		}

		else if (type.toLowerCase().equals("tree")) {
			this.setName("Tree");
			this.life = 150;
			this.attack = 0;
			this.range = 0;
			this.cost = 50;
			this.size = 1;
			this.travel = 0;
			this.point = 250;
		}

		else if (type.toLowerCase().equals("house")) {
			this.setName("House");
			this.life = 1000;
			this.attack = 0;
			this.range = 0;
			this.cost = 200;
			this.size = 2;
			this.travel = 0;
			this.point = 0;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	};

	public String getType() {
		return this.type;
	}

	public boolean attacked(int damage) {
		int tmp = this.damage + damage;
		if (tmp > this.life) {
			this.damage = this.life;
			return true;
		} else {
			this.damage = tmp;
			return false;
		}
	}
	
	public void print() {		
		System.out.print("{" + this.user.getId() + "," + this.name + "," + this.type + "," + this.image + "," +
						 this.life +"," + this.damage + "," + this.attack + "," + this.range + "," +
						this.cost + "," + this.size + "," + this.travel + "," + this.point +"," + this.id + "}");
	}
	
	public int getAttack() {
		return this.attack;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setDamage(int damage) {
		this.damage = damage;
	}
	
	public int getId() {
		return this.id;
	}
	
	public int getLifeRest() {
		return this.life - this.damage;
	}
	
	public int getPoint() {
		return this.point;
	}
	
	public void save() {
		Mysql bd = new Mysql();
		String strsql;
		
		strsql = "insert into tb_components (user_id,name,type,image,life,damage,attack,rangeatt,cost,travel,point) values" +
				"("+this.user.getId()+",'"+this.name+"','"+this.type+"','"+this.image+"',"+this.life+","+
				this.damage+"," + this.attack + ","+ this.range + "," + this.cost + ","+this.travel+","+this.point + ")";
		
		ResultSet rs = bd.insertUpdate(strsql);
		try {
			rs.next();
			this.id = rs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
